package com.project8.main;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList; 
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class BookListUpdater extends AsyncTask<Void, Void, Void> 
{
	  	
	private ProgressDialog progress;
	private Context context;
		 
	//Variables for Passing to Download list
	ArrayList<String> bookList = new ArrayList<String>();
	ArrayList<String> bookDescriptions = new ArrayList<String>();
	ArrayList<Bitmap> bookImages = new ArrayList<Bitmap>();
	
 

	public BookListUpdater(ProgressDialog progress, Context context) 
	{
		this.progress = progress;
		this.context = context;
	}

	public void onPreExecute() 
	{
		progress.show();
	}

	public Void doInBackground(Void... unused) 
	{
		getBooks();
          
		return null;
	}  

	public void onPostExecute(Void unused) 
	{
	    progress.dismiss();
	    
	    //Bundle Up Books Recieved
	    Bundle bun = new Bundle();
	    bun.putSerializable("BookList", bookList);
	    bun.putSerializable("BookImages", bookImages);
	    bun.putSerializable("BookDescriptions", bookDescriptions);
	
	    //Start Book List
	    Intent myIntent = new Intent(context, BookList.class);
	    myIntent.putExtras(bun); 
	    myIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
	    context.startActivity(myIntent);
	}
  
	public void getBooks()
	{
		try
		{
			URL bookURL = new URL("http://www.cis.umassd.edu/~jplante/textbooks/");
			//URL bookURL = new URL("http://www.cis.umassd.edu/~bcollins/books/");
  	        
			URLConnection con = bookURL.openConnection();
  	        
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  	        String inputLine;

  	        while ((inputLine = in.readLine()) != null)
  	        {
  	            if(inputLine.indexOf("ITextbook_") > 0)
  	            {
  	            	System.out.println("BOOKS FOR DOWNLOAD: " + inputLine);
  	            	
	  	            String book = inputLine.substring(inputLine.indexOf("\"ITextbook_") + 11, inputLine.indexOf("/\""));
	  	            Log.d("Book List", "BOOK = " + book);
	  	            
	  	            //URL infoURL = new URL("http://www.cis.umassd.edu/~bcollins/books/ITextbook_" + book + "/info.txt");
	  	            URL infoURL = new URL("http://www.cis.umassd.edu/~jplante/textbooks/ITextbook_" + book + "/info.txt");
	  	            URLConnection infoCon = infoURL.openConnection();
	  	            
	  	            //URL thumbnailURL = new URL("http://www.cis.umassd.edu/~bcollins/books/ITextbook_" + book + "/thumbnail.png");
	  	            URL thumbnailURL = new URL("http://www.cis.umassd.edu/~jplante/textbooks/ITextbook_" + book + "/thumbnail.png");
	  	            URLConnection imageCon = thumbnailURL.openConnection();
	  	            
	  	            
	  	            Bitmap bm = BitmapFactory.decodeStream(imageCon.getInputStream());
	  	            bookImages.add(bm);
	  	             
	  	            BufferedReader inInfo = new BufferedReader(new InputStreamReader(infoCon.getInputStream()));
	    	        String infoInput;
	    	        String info = "";
	    	        
	    	        while ((infoInput = inInfo.readLine()) != null)
	      	        {
	    	        	info  = info + infoInput + "\n";
	    	        	
	      	        }
	  	            
	    	        bookDescriptions.add(info);
	  	            bookList.add(book);
  	            }
  	            
  	        }
  	        
  	        in.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}//End getBooks		 
}